// haunted.txt
// Exactly like basicnpc.txt (the default creature script), with one 
// major difference. If this creature is ever hostile, it will immediately 
// hunt the party down. This script is for creatures that hunt down party from
// far away. Since the pathing engine can't move a character too far, this
// character will go to an intervening waypoint before tracking the party.

// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - The waypoint the character walks to before tracking the party.


begincreaturescript;

variables;

short i,respawn,r1,target;
short got_to_wayp = 0;
short party_sighted = 0;
short last_abil_time;

body;

beginstate INIT_STATE;

set_script_mode(3);
alert_char(ME);

approach_char(ME,random_group_member(0),2);

break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);

place_monster(my_loc_x(),my_loc_y(),239,0);
play_sound(164);
erase_char(ME);

if (get_sdf(5,0) == 0) {
 force_instant_terrain_redraw();
 message_dialog("You strike down the haunted creature. It falls back with a large tear in its ethereal structure. Its form begins to undergo a sort of boiling as it quickly evaporates.","A brightly glowing wisp of light emerges from the phase change. It quickly begins to fly off. You notice the dark thoughts are much weaker now.");
}

inc_flag(5,0,1);

break;

beginstate START_STATE; 

// If on pathing and site party, stop movement, and attack
if ((can_see_char(1000) ==1) && (get_attitude(ME) >= 10) && (party_sighted == 0)) {
 party_sighted = 1;
 stop_moving(ME);
 set_target(ME,1000);
}

	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}

// Haunted Description
if (get_sdf(1,15) == 0) {
 set_flag(1,15,1);
 message_dialog("Just ahead you see an eeriely glowing Nephilim. It looks at you and begins to move toward you with motions much like a puppet. It does not seem hostile, yet, at least.","");
}

// Make hostile if leave conversation early
 if ((get_sdf(1,14) > 0) && (get_attitude(ME) < 10))
  set_attitude(ME,10);


// Make conversation with party
	if ((can_see_char(1000) > 0) && (get_attitude(ME) < 10)) {
		party_sighted = 1;
	if ((get_flag(1,14) == 0) && (dist_to_party() <= 8) && (party_sighted > 0)) {
		if (dist_to_party() <= 3) {
			set_flag(1,14,1);
			begin_talk_mode(1);
			}
			else approach_char(ME,random_group_member(0),2);
		}
}
		
	if (get_attitude(ME) >= 10) {
		if (got_to_wayp == 0) {
			if (approach_waypoint(ME,get_memory_cell(4),2))
				if (dist_to_waypoint(ME,get_memory_cell(4)) <= 6) {
					got_to_wayp = 1;
					alert_char(ME);
					}
			}
			else if (dist_to_party() <= 20)
				alert_char(ME);
		}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((tick_difference(last_abil_time,get_current_tick()) >= 3) && (get_ran(1,0,100) < 50) && (can_see_char(1000))) {
	  	if ((get_sdf(5,0) == 1) || (get_sdf(5,0) == 2)) {
			print_named_str(ME,"fires a draining beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(46);
			set_char_status(get_target(),1,-5,0,1);
	  		}
	  	if ((get_sdf(5,0) == 3) || (get_sdf(5,0) == 4)) {
			print_named_str(ME,"fires an enfeebling beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(65);
			set_char_status(get_target(),21,5,0,1);
	  		}
	  	if (get_sdf(5,0) == 5) {
			print_named_str(ME,"fires a paralyzing beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(7);
			set_char_status(get_target(),11,2,0,1);
	  		}
		last_abil_time = get_current_tick();
		}
	if ((tick_difference(last_abil_time,get_current_tick()) >= 3) && (get_ran(1,0,100) < 50) && (can_see_char(1000))) {
	  	if ((get_sdf(5,0) == 1) || (get_sdf(5,0) == 2)) {
			print_named_str(ME,"is blessed.");
			put_sparkles_on_char(ME,2,10);
			run_animation_sound(4);
			set_char_status(ME,1,10,0,1);
	  		}
	  	if ((get_sdf(5,0) == 3) || (get_sdf(5,0) == 4)) {
			print_named_str(ME,"speeds up.");
			put_sparkles_on_char(ME,4,10);
			run_animation_sound(4);
			set_char_status(ME,3,10,0,1);
	  		}
	  	if (get_sdf(5,0) >= 5) {
			print_named_str(ME,"is divinely touched!");
			put_sparkles_on_char(ME,6,10);
			run_animation_sound(12);
			set_char_status(ME,16,5,0,1);
	  		}
		last_abil_time = get_current_tick();
		}
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;